package com.jz.warehouseareaclass.service.impl;

import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import com.jz.warehouseareaclass.mapper.WarehouseareaMapper;
import com.jz.warehouseareaclass.model.*;
import com.jz.warehouseareaclass.service.WarehouseareaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class WarehouseareaServiceImpl implements WarehouseareaService {

    @Autowired
    private WarehouseareaMapper warehouseareaMapper;

    @Override
    public ResultVo firstPage(WarehouseareaSelectVo warehouseareaSelectVo) {
        List<WarehouseareaVo> warehouseareaVoList = warehouseareaMapper.queryData(warehouseareaSelectVo);
        //查询数量
        for (WarehouseareaVo warehouseareaVo : warehouseareaVoList) {

            int status = warehouseareaVo.getStatus() ? 1 : 0;
            warehouseareaVo.setStatusName(SubStatusEnum.getNameByStatus(status));
        }
        int count = warehouseareaMapper.queryCount(warehouseareaSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(warehouseareaVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(warehouseareaSelectVo.getPageIndex());
        allRecordsVo.setPageSize(warehouseareaSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addpartClass(WarehouseareaAddVo partAddVo) {
        //验证编码是否存在
        WarehouseareaVo warehouseareaVO = warehouseareaMapper.queryByCode(partAddVo.getCode());
        if (warehouseareaVO != null) {
            return ResultBuildVo.error("库区编码已经存在！");
        }
        //验证名称是否存在
        WarehouseareaVo subClassName = warehouseareaMapper.queryByName(partAddVo.getName());
        if (subClassName != null) {
            return ResultBuildVo.error("库区名称已经存在！");
        }
        Date nowDate = new Date();
        WarehouseareaVo classVo = new WarehouseareaVo();
        BeanUtils.copyProperties(partAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        warehouseareaMapper.add(classVo);
        warehouseareaMapper.add1(classVo);
        return ResultBuildVo.success();
    }

    @Override
    public ResultVo updateSubClass(@Valid WarehouseareaUpdateVo partUpdateVo) {
        List<WarehouseareaVo> warehouseareaVo = warehouseareaMapper.queryById(partUpdateVo.getId());
        if (warehouseareaVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        WarehouseareaVo subClassName = warehouseareaMapper.queryByName(partUpdateVo.getName());
        if (subClassName != null && partUpdateVo.getId() != subClassName.getId()) {

            return ResultBuildVo.error("修改名称已存在");
        }
        WarehouseareaVo warehouseareaVo1 = warehouseareaMapper.queryByCode(partUpdateVo.getCode());
        if (warehouseareaVo1 != null && partUpdateVo.getId() != warehouseareaVo1.getId()) {

            return ResultBuildVo.error("修改编码已存在");
        }

        Date nowDate = new Date();
        WarehouseareaVo classVo = new WarehouseareaVo();
        BeanUtils.copyProperties(partUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        warehouseareaMapper.update(classVo);
        List<WareReservoir> wareReservoirList = warehouseareaMapper.selectFist(classVo);
        if (!CollectionUtils.isEmpty(wareReservoirList)){

            warehouseareaMapper.deleteFirst(classVo);
        }
        warehouseareaMapper.add1(classVo);
//        if (version == 0){
//
//            return ResultBuildVo.error("已被修改，刷新重试");
//        }
        return ResultBuildVo.success();

    }

    @Override
    public ResultVo updateStatus(@Valid WarehouseareaUpdateStatusVo warehouseareaUpdateStatusVo) {
        List<WarehouseareaVo> warehouseareaVo = warehouseareaMapper.queryById(warehouseareaUpdateStatusVo.getId());
        if (warehouseareaVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        WarehouseareaVo classVo = new WarehouseareaVo();
        BeanUtils.copyProperties(warehouseareaUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        warehouseareaMapper.updateStatus(classVo);
        return ResultBuildVo.success();

    }

    @Override
    public ResultVo queryStorageSectionById(@Valid Integer id) {

      List<WarehouseareaVo> warehouseareaVoList = warehouseareaMapper.queryById(id);
        return ResultBuildVo.success(warehouseareaVoList);
    }

}

